package Entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import Control.MySQLController;

public class Shop_Order {
	
	private String orderId;
	private Date orderDate;
	private Date collectDate;
	private double totalPrice;
	private String needyNric;
	private String customerNric;
	private Shop_Order_StatusCode statusCode = new Shop_Order_StatusCode();
	
	private ArrayList <Shop_Customization> customizations = new ArrayList <Shop_Customization> ();
	
	public Shop_Order(){}
	
	public Shop_Order(String orderId){
		this.setOrderId(orderId);
	}
	
	public Shop_Order(String orderId, Date orderDate, Date collectDate, double totalPrice, String nric, String orderStatusCode){
		this.setOrderId(orderId);
		this.setOrderDate(orderDate);
		this.setCollectDate(collectDate);
		this.setTotalPrice(totalPrice);
		this.setCustomerNric(nric);
		this.getStatusCode().setOrderStatusCode(orderStatusCode);
	}
	
	public Shop_Order(String orderId, Date orderDate, Date collectDate, double totalPrice, String nric, String needyNric, String orderStatusCode){
		this.setOrderId(orderId);
		this.setOrderDate(orderDate);
		this.setCollectDate(collectDate);
		this.setTotalPrice(totalPrice);
		this.setCustomerNric(nric);
		this.setNeedyNric(needyNric);
		this.getStatusCode().setOrderStatusCode(orderStatusCode);
	}
	
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public Date getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
	public Date getCollectDate() {
		return collectDate;
	}
	public void setCollectDate(Date collectDate) {
		this.collectDate = collectDate;
	}
	public double getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}
	public String getNeedyNric() {
		return needyNric;
	}

	public void setNeedyNric(String needyNric) {
		this.needyNric = needyNric;
	}

	public String getCustomerNric() {
		return customerNric;
	}

	public void setCustomerNric(String customerNric) {
		this.customerNric = customerNric;
	}

	public Shop_Order_StatusCode getStatusCode() {
		return statusCode;
	}
	public void setStatusCode(Shop_Order_StatusCode statusCode) {
		this.statusCode = statusCode;
	}

	/**
	 * @return the customizations
	 */
	public ArrayList <Shop_Customization> getCustomizations() {
		return customizations;
	}

	/**
	 * @param customizations the customizations to set
	 */
	public void setCustomizations(ArrayList <Shop_Customization> customizations) {
		this.customizations = customizations;
	}
	
	public static ArrayList<Shop_Order>[] getOrdersByCustomer(String customerNric){
		@SuppressWarnings("unchecked")
		ArrayList<Shop_Order>[] orderStacks = new ArrayList[3];
		
		ArrayList<Shop_Order> ready = new ArrayList<Shop_Order>();
		ArrayList<Shop_Order> pending = new ArrayList<Shop_Order>();
		ArrayList<Shop_Order> collected = new ArrayList<Shop_Order>();
		
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.order_form INNER JOIN ooadpj.order_status ON order_form.order_status_code = order_status.order_status_code WHERE customer_nric = '" + customerNric + "' ORDER BY order_form.collection_date";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Order order = new Shop_Order(rs.getString("order_id"), rs.getDate("order_date"), rs.getDate("collection_date"), rs.getDouble("total_price"), rs.getString("customer_nric"), rs.getString("needy_nric"), rs.getString("order_status_code"));
				order.retrieveCustomizations();
				
				if(rs.getString("order_status.description").equals("Ready")){
					ready.add(order);
				}
				else if(rs.getString("order_status.description").equals("Pending") || rs.getString("order_status.description").equals("Accepted")){
					pending.add(order);
				}
				else if(rs.getString("order_status.description").equals("Collected")){
					collected.add(order);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		orderStacks[0] = ready;
		orderStacks[1] = pending;
		orderStacks[2] = collected;
		
		return orderStacks;
	}
	
	public static ArrayList<Shop_Order> getAllOrders(){
		ArrayList<Shop_Order> orders = new ArrayList<Shop_Order>();
		
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.order_form INNER JOIN ooadpj.order_status ON order_form.order_status_code = order_status.order_status_code ORDER BY order_form.collection_date";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Order order = new Shop_Order(rs.getString("order_id"), rs.getDate("order_date"), rs.getDate("collection_date"), rs.getDouble("total_price"), rs.getString("customer_nric"), rs.getString("needy_nric"), rs.getString("order_status_code"));
				order.retrieveCustomizations();
				orders.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		return orders;
	}

	public static ArrayList<Shop_Order> getNeedyOrders(String nric){
		ArrayList<Shop_Order> orders = new ArrayList<Shop_Order>();
		
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.order_form WHERE needy_nric = '"+nric+"' ORDER BY order_form.collection_date";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Order order = new Shop_Order(rs.getString("order_id"), rs.getDate("order_date"), rs.getDate("collection_date"), rs.getDouble("total_price"), rs.getString("customer_nric"), rs.getString("needy_nric"), rs.getString("order_status_code"));
				order.retrieveCustomizations();
				orders.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		return orders;
	}
	
	public static ArrayList<User> getPossibleNeedy(Shop_Order orderToTakeUp){
		User temp = new User();
		ArrayList<User> users = temp.retrieveAllUsers();
		ArrayList<User> usersToRemove = new ArrayList<User>();
		
		for(int i = 0; i < users.size(); i++){
			ArrayList<String> categoriesNeedy = Workshop_BookWorkshopEntity.retrieveOBSBookingsCategories(users.get(i).getNric());
			boolean linked = false;
			for(int ii = 0; ii < orderToTakeUp.getCustomizations().size(); ii++){
				for(int iii = 0; iii < categoriesNeedy.size(); iii++){
					if(orderToTakeUp.getCustomizations().get(ii).getProduct().getWorkshopId() .equals("NONE")){
						linked = true;
						break;
					}
					else{
						if(categoriesNeedy.get(iii).equals(orderToTakeUp.getCustomizations().get(ii).getProduct().getWorkshopId())){
							linked = true;
							break;
						}
					}
				}
			}
			if(!linked){
				usersToRemove.add(users.get(i));
			}
		}
		System.out.println(usersToRemove.size());
		
		for(int i = 0; i < usersToRemove.size(); i++){
			users.remove(usersToRemove.get(i));
		}
		
		return users;
	}
	
	public void retrieveCustomizations(){
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.customization INNER JOIN ooadpj.product ON customization.product_id = product.product_id WHERE order_id = '" + orderId + "'";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Customization customization = new Shop_Customization();
				customization.setProduct(new Shop_Product(rs.getString("product_id")));
				customization.getProduct().setWorkshopId(rs.getString("workshop_id"));
				customization.setCustId(rs.getString("cust_id"));
				customization.setOrderId(rs.getString("order_id"));
				customization.setFrontCustUrl(rs.getString("front_cust_url"));
				customization.setBackCustUrl(rs.getString("back_cust_url"));
				customization.setQuantity(rs.getInt("quantity"));
				customization.setCustStatusCode(new Shop_CustomizationStatusCode(rs.getString("cust_status_code")));
				getCustomizations().add(customization);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
	}
	
	public boolean updateOrderStatus(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.order_form SET order_status_code = '" + statusCode.getOrderStatusCode() + "' WHERE order_id = '" + orderId + "'";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		
		return success;
	}
	
	public boolean acceptOrder(String nric){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.order_form SET needy_nric = '" + nric + "', order_status_code = '" + statusCode.getOrderStatusCode() + "' WHERE order_id = '" + orderId + "'";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		
		return success;
	}
	
	public boolean createOrder(){
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.order_form (order_id, order_date, collection_date, total_price, customer_nric, order_status_code) VALUES ('" + orderId + "', '" + sdf.format(orderDate) + "', '" + sdf.format(collectDate) + "', " + totalPrice + ", '" + customerNric + "', 'PEN')";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		
		return success;
	}

}
